package com.hgyc.mom.mes.controller;

import java.util.*;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import com.hgyc.mom.mes.entity.SopTerminal;
import com.hgyc.mom.mes.vo.SopTerminalParam;
import com.hgyc.mom.mes.service.SopTerminalService;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 显示终端 Controller
 *
 * @author fwj
 * @since 2025-06-24
 */
@Tag(name = "显示终端 控制器", description = "显示终端 控制器")
@RestController
@RequestMapping("/mes/terminal")
public class SopTerminalController {
    @Autowired
    private SopTerminalService sopTerminalService;

    @Operation(summary = "查询显示终端 列表", description = "查询显示终端 列表")
    @PostMapping("/list")
    public Result<List<SopTerminal>> list(@Parameter(description = "查询参数") @RequestBody SopTerminalParam sopTerminalParam) {
        SopTerminal sopTerminal = new SopTerminal();
        BeanUtils.copyProperties(sopTerminalParam, sopTerminal);
        QueryWrapper<SopTerminal> wrapper = QueryWrapperUtil.build(sopTerminal);
        List<SopTerminal> list = sopTerminalService.list(wrapper);
        return Result.success(list);
    }

    @Operation(summary = "查询显示终端 列表", description = "查询显示终端 列表")
    @PostMapping("/pageList")
    public Result<IPage<SopTerminal>> pageList(@Parameter(description = "查询参数") @RequestBody SopTerminalParam sopTerminalParam) {
        Page<SopTerminal> page = new Page<>(sopTerminalParam.getPage(), sopTerminalParam.getPageSize());
        SopTerminal sopTerminal = new SopTerminal();
        BeanUtils.copyProperties(sopTerminalParam, sopTerminal);
        QueryWrapper<SopTerminal> wrapper = QueryWrapperUtil.build(sopTerminal);
        IPage<SopTerminal> list = sopTerminalService.page(page, wrapper);
        return Result.success(list);
    }

    @Operation(summary = "获取显示终端 详细信息", description = "获取显示终端 详细信息")
    @GetMapping(value = "/{id}")
    public Result getInfo(@Parameter(description = "显示终端 ID") @PathVariable("id") Long id) {
        return Result.success(sopTerminalService.getById(id));
    }
    @Operation(summary = "新增显示终端 ", description = "新增显示终端 ")
    @PostMapping
    public Result<?> save(@Parameter(description = "显示终端 ") @RequestBody SopTerminal sopTerminal) {
        sopTerminalService.saveTerminal(sopTerminal);
        return Result.success();
    }

    @Operation(summary = "修改显示终端 ", description = "修改显示终端 ")
    @PutMapping
    public Result<?> update(@Parameter(description = "显示终端 ") @RequestBody SopTerminal sopTerminal) {
        sopTerminalService.updateTerminal(sopTerminal);
        return Result.success();
    }

    @Operation(summary = "删除显示终端 ", description = "删除显示终端 ")
    @DeleteMapping
    public Result<?> remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        sopTerminalService.removeTerminal(Arrays.asList(ids));
        return Result.success();
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<SopTerminal> util = new ExcelUtil<>(SopTerminal.class);
        util.importTemplateExcel(response, "显示终端 ");
    }

    @Operation(summary = "导出显示终端 列表", description = "导出显示终端 列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SopTerminalParam sopTerminalParam) {
        SopTerminal sopTerminal = new SopTerminal();
        BeanUtils.copyProperties(sopTerminalParam, sopTerminal);
        QueryWrapper<SopTerminal> wrapper = QueryWrapperUtil.build(sopTerminal);
        List<SopTerminal> list = sopTerminalService.list(wrapper);
        ExcelUtil<SopTerminal> util = new ExcelUtil<>(SopTerminal.class);
        util.exportExcel(response, list, "显示终端 数据");
    }

    @Operation(summary = "导入显示终端 列表", description = "导入显示终端 列表")
    @PostMapping("/import")
    public Result<?> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<SopTerminal> util = new ExcelUtil<>(SopTerminal.class);
        List<SopTerminal> sopTerminalList = util.importExcel(file.getInputStream());
        String message = sopTerminalService.importData(sopTerminalList, updateSupport);
        return Result.success(message);
    }
}
